<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Auth;

class CouponController extends Controller
{
    /**
     * 新增优惠券
     */
    public function getAdd()
    {
        $sid = session()->get('sname')['id'];
        return view('shop.coupon.add',['sid'=>$sid]);
    }
    /**
     * 执行新增优惠券
     * @return [type] [description]
     */
    public function postAdd(Request $request)
    {
        $data['sid'] = $request->sid;
        $data['money'] = $request->money;
        $data['overdue'] = time()+$request->overdue*2592000;
        $data['condi'] = $request->condi;
        DB::table('coupons')->insert($data);
        return redirect('/coupon/list');
    }

    /**
     * 优惠券列表
     */
    public function getList()
    {
        $coupons = DB::table('coupons')->get();
        return view('shop.coupon.list',['coupons'=>$coupons]);
    }

    /**
     * 删除优惠券
     */
    public function getDelete($id)
    {
        DB::table('coupons')->where('id','=',$id)->delete();
        return back();
    }

    /**
     * 用户领取优惠券
     */
    public function getLing(Request $request)
    {
        $cid = $request->id;
        $uid = Auth::user()->id;
        if(Auth::check()){
            DB::table('user_coupons')->insert(['uid'=>$uid,'cid'=>$cid]);
            echo "已领取";
        }else{
            echo "请先登陆";
        }

    }
}
